App.controller('trackController', ['$scope', '$state', '$stateParams', 'Page', 'Form', function($scope, $state, $stateParams, Page, Form) {

    $scope.process = $stateParams.process || {};
    $scope.task = $stateParams.task || {};
    $scope.properties = [];
    $scope._properties = {};
    $scope.selectAssignee = false;
    $scope.print = false;
    $scope.$watch('properties', function (newValue, oldValue) {
        if (newValue && newValue.length > 0) {
            $scope._properties = {};
            $(newValue).each(function(index, property) {
                $scope._properties[property.id] = property;
            });
        }
    });

    $scope.cancel = function() {// 返回
    	 window.history.back();
    };

    if ($stateParams.processInstanceId || $scope.task.processInstanceId){
    	$scope.processInstanceId = $stateParams.processInstanceId;
    	Page.ajaxGet('bpm/process/' + $scope.processInstanceId + '/view.do', null, function(resp) {
    		var processDefinitionId = (resp.data.processDefinition && resp.data.processDefinition.id) ? resp.data.processDefinition.id : '';
            $scope.key = resp.data.key;
            $scope.task.from = $scope.task.from || resp.data.form;
            $scope.process.form = resp.data.form;
            $scope.comments = resp.data.comments;
            $scope.processPath = 'views/bpm/diagram-viewer/index.html?processDefinitionId=' + processDefinitionId + '&processInstanceId=' + $scope.processInstanceId;
        });
    } else {
        window.history.back();
        return;
    }
    
    $scope.bakPrint = function(){
    	if($scope.processInstanceId){
    		var dataSourceUrl = 'bpm/process/' + $scope.processInstanceId + '/print.do';
            window.open("views/official/printWithTemplate.jsp?dataSourceUrl=" + dataSourceUrl ,"_blank");
    	}else{
    		Page.alert("参数错误!");
    	}
    };
    
    $scope.processTrackGrid = {
        	options: {
	            scroller: true,
	            paging: false   
            }
    };

}]);

App.directive('edBpmInput', ['$parse', '$compile', function ($parse, $compile) {
    return {

        restrict: 'A',

        controller: function($scope, $element, $rootScope){
        },

        link: function (scope, elem, attrs) {

            var model = attrs.ngModel;
            var index = model.lastIndexOf('.');
            model = model.substring(index + 1, model.length);
            // 添加name属性
            $(elem).attr("name", model);

            scope.$watch('_properties', function (newValue, oldValue) {
                if (newValue) {
                    var prop = newValue[model];
                    if (prop) {
                    	//规定:可读并且必须时才是readonly
                        if (prop.readable === true && prop.required ===true) {
                            $(elem).attr("readonly","readonly");
                            return;
                        }
                    }
                }
            });
        }
    };
}]);
